//
//  KeyWordDao.m
//  SouthChina
//
//  Created by creator_lq on 15/1/18.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "KeyWordDao.h"
#import "KeyWords.h"

@implementation KeyWordDao

+(BOOL)updateKeyBooks:(NSString*)kId word:(NSString*)words{
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * delsql = [NSString stringWithFormat:@"DELETE keybooks messagesession where kid='%@'",kId];
    [_dataBase executeUpdate:delsql];
    
    NSString *sql = @"INSERT INTO keybooks (kid,words,OTHER_INFO,UPDATE_TIME) VALUES (?,?,?,?)";
    
    BOOL a =[_dataBase executeUpdate:sql,kId,words,nil,[NSDate date]];
    
    if(!a){
        NSLog(@"updateKeyBooks 失败");
    }else{
        NSLog(@"updateKeyBooks 成功");
        
    }
    [_dataBase close];
    
    return a;

    
}

+(NSString*)queryKeyBooksById:(NSString*)kwid{
    
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
     NSString * sql = @"SELECT * from keybooks where kid=?";
     FMResultSet *rs=[_dataBase executeQuery:sql,kwid];
    NSString *words = @"";
    while ([rs next]) {
        words =[rs stringForColumn:KWORDS];
    };
    [rs close];
    [_dataBase close];
    return words;

}


+(BOOL)checkTableCreatedInDb:(FMDatabase *)db
{
    NSString *createStr=@"CREATE  TABLE  IF NOT EXISTS 'keybooks' ('kid' text PRIMARY KEY  NOT NULL  UNIQUE ,'words'  text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    return worked;
    
}


@end
